package com.example.statistics.ui.bank

import android.os.Bundle
import com.example.statistics.bean.Bank
import com.example.statistics.bean.DefineInputBean
import com.example.statistics.bean.AdjustmentBean
import com.example.statistics.common.BaseInputActivity
import com.example.statistics.ex.getSync
import com.example.statistics.ex.loadingDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.weight.TextProcessingView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class AdjustmentAddActivity : BaseInputActivity<AdjustmentBean>() {

    private var bankList: List<Bank>? = null

    override val title: String
        get() = "调账转账"

    override val url: String
        get() = UrlConfig.BANK_ADJUSTMENT_ADD

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val dialog = loadingDialog()
        GlobalScope.launch {
            bankList = getSync<List<Bank>>(UrlConfig.BANK_ACCOUNT_LIST)
            withContext(Dispatchers.Main) {
                dialog.dismiss()
                initView()
                refresh()
            }
        }
    }

    override val inputDataList: List<DefineInputBean>
        get() = listOf(
            DefineInputBean("收款账户", "_collectionAccount", TextProcessingView.STYLE_SELECTOR,
                singleDialogT = Pair(bankList.orEmpty()) { index, _ ->
                    val bank = bankList?.get(index)
                    bank?.let {
                        extentParams.add("collectionAccountId" to it.ID)
                    }
                }),
            DefineInputBean("付款账户", "_paymentAccount", TextProcessingView.STYLE_SELECTOR,
                singleDialogT = Pair(bankList.orEmpty()) { index, _ ->
                    val bank = bankList?.get(index)
                    bank?.let {
                        extentParams.add("paymentAccountId" to it.ID)
                    }
                }),
            DefineInputBean("金        额", "money", TextProcessingView.STYLE_INPUT_FLOAT),
            DefineInputBean("备        注", "money", TextProcessingView.STYLE_INPUT)
        )
}